package net.jielan188.hd.util.map;

import net.jielan188.hd.util.Log;
import org.apache.commons.lang.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.math.BigDecimal;

/**
 * Map工具类
 *@author huangguogang
 *@date 2017-01-23
 */

public class MapUtils {

  /**
   * Class转Map
  */
  public static Map<String,Object> getValue( Object thisObj ){

    Map<String,Object> map = new HashMap();
    Class c;
    try{
      c = Class.forName( thisObj.getClass().getName() );
      Method[] m = c.getMethods();
      for ( int i = 0; i < m.length; i++ ){
        String method = m[i].getName();
        if ( method.startsWith("get") ){
          try{
            Object value = m[i].invoke( thisObj );

            if (value != null){
              String key=method.substring(3);
              key=key.substring(0,1).toLowerCase()+key.substring(1);
              map.put( key, value );
            }
          }catch ( Exception e ) {
            Log.info( "转换Map 发生异常：" + e.getMessage() );
          }
        }
      }
    }catch ( Exception e ) {
      Log.info( "转换Map 发生异常：" + e.getMessage() );
    }
    return map;
  }

  /**
   * Class转Map
  */
  public static Object mapToClass( Object o,Map<String,String> objectMap ){

    if( o == null || objectMap == null ){
      return null;
    }
    Class thisClass;
    try {
      thisClass = Class.forName( o.getClass().getName() );
      Field[] field = thisClass.getDeclaredFields();
      for ( int i = 0 ; i < field.length ; i ++ ){

        String attributeName = field[i].getName();
        String attributeType = field[i].getGenericType().toString();
        String attributeName1 = attributeName.substring( 0,1 ).toUpperCase() + attributeName.substring( 1 );
        if( StringUtils.equalsIgnoreCase(attributeType, "class java.lang.String") ){
          if( objectMap.get( attributeName ) != null ){
            Method method = o.getClass().getMethod( "set" + attributeName1,String.class );
              method.invoke( o, objectMap.get( attributeName ) );
            }
          }else if( StringUtils.equalsIgnoreCase( attributeType,"class java.lang.Integer" ) ){
            if( objectMap.get( attributeName ) != null ) {
              Method method = o.getClass().getMethod( "set" + attributeName1, Integer.class );
              method.invoke( o, Integer.parseInt(objectMap.get( attributeName ) ) );
            }
          }else if( StringUtils.equalsIgnoreCase( attributeType,"class java.lang.Double" ) ){
            if( objectMap.get( attributeName ) != null ){
              Method method = o.getClass().getMethod( "set" + attributeName1,Double.class );
              method.invoke( o, Double.parseDouble( objectMap.get( attributeName ) ) );
            }
          }else if( StringUtils.equalsIgnoreCase( attributeType,"class java.lang.Boolean" ) ){
            if( objectMap.get( attributeName ) != null ){
              Method method = o.getClass().getMethod( "set" + attributeName1,Boolean.class );
              method.invoke( o, Boolean.parseBoolean( objectMap.get( attributeName ) ) );
            }
          }else if( StringUtils.equalsIgnoreCase( attributeType,"class java.util.Date" ) ){
            if( objectMap.get( attributeName ) != null ){
              Method method = o.getClass().getMethod( "set" + attributeName1,Date.class );
              method.invoke( o, new Date() );
            }
          }else if( StringUtils.equalsIgnoreCase( attributeType,"class java.math.BigDecimal" ) ){
            if( objectMap.get( attributeName ) != null ){
              Method method = o.getClass().getMethod( "set" + attributeName1,BigDecimal.class );
              method.invoke( o, 1 );
            }
          }
        }
      }catch ( Exception e ){
        Log.error( "conversion class happen a error" );
        return null;
      }
      return o;
    }
}
